/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.sdRankingSys.Handlers;

import com.SySammy.SdCurrency.sdCCommandExport;
import com.SySammy.sdRankingSy.PersistenceClasses.Players;
import com.SySammy.sdRankingSys.sdRS;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

public class PerksHandler {

    sdRS plugin;
    sdCCommandExport currency = new sdCCommandExport();
    yamlHandler YH = new yamlHandler(plugin);

    public PerksHandler(sdRS plugin) {
        this.plugin = plugin;
    }

    private Players OpenDataBase(Player ply) {
        String p = ply.getName();
        Players persistenceClass = plugin.getDatabase().find(Players.class).where().ieq("PlayerName", p).findUnique();
        if (persistenceClass == null) {
            persistenceClass = new Players();
        }
        return persistenceClass;
    }

    public int NumbOfPerks(Player ply) {
        Players plyReg = OpenDataBase(ply);
        return plyReg.getRanks().getNumbPerks();
    }

    public void ListOfPerks(Player ply) {
        Players plyReg = OpenDataBase(ply);
        List<String> permissions = YH.getPerks(plyReg.getRanks().getVocation(), plyReg.getRanks().getVocationLvl());
        ply.sendMessage(ChatColor.DARK_AQUA + "List of available perks:");
        if (permissions.isEmpty()) {
            ply.sendMessage(ChatColor.RED + "You don't have perks yet.");
            return;
        }
        for (int x = 0; x < permissions.size(); x++) {
            ply.sendMessage(ChatColor.GOLD + permissions.get(x));
        }
    }

    public String PerkInfo(Player ply, int slot) {
        Players plyReg = OpenDataBase(ply);
        if (slot == 1) {
            return plyReg.getRanks().getPerk1();
        }
        if (slot == 2) {
            return plyReg.getRanks().getPerk2();
        }
        if (slot == 3) {
            return plyReg.getRanks().getPerk3();
        }
        return null;
    }

    public void RegPerk(Player ply, int perkslot, String perk) {
        Players plyReg = OpenDataBase(ply);
        int perkQnt = YH.getPerkQnt(plyReg.getRanks().getVocation(), plyReg.getRanks().getVocationLvl());
        if (perkQnt >= 0) {
            SetPermission(ply, perk, perkslot);
        }
        plugin.getDatabase().save(plyReg);
    }

    public void RemovePerk(Player ply, int perkslot) {
        Players plyReg = OpenDataBase(ply);
        int perkQnt = YH.getPerkQnt(plyReg.getRanks().getVocation(), plyReg.getRanks().getVocationLvl());
        if (perkQnt >= 0) {
            if (perkslot == 1) {
                RemovePermission(ply, plyReg.getRanks().getPerk1());
                plyReg.getRanks().setPerk1("null");
            } else if (perkslot == 2) {
                RemovePermission(ply, plyReg.getRanks().getPerk2());
                plyReg.getRanks().setPerk2("null");
            } else if (perkslot == 3) {
                RemovePermission(ply, plyReg.getRanks().getPerk3());
                plyReg.getRanks().setPerk3("null");
            }
        }
        plugin.getDatabase().save(plyReg);
    }

    public void RemoveAllPerks(Player ply) {
        Players plyReg = OpenDataBase(ply);
        RemovePermission(ply, plyReg.getRanks().getPerk1());
        plyReg.getRanks().setPerk1("null");
        RemovePermission(ply, plyReg.getRanks().getPerk2());
        plyReg.getRanks().setPerk2("null");
        RemovePermission(ply, plyReg.getRanks().getPerk3());
        plyReg.getRanks().setPerk3("null");
        plugin.getDatabase().save(plyReg);
    }

    public void SetPermission(Player p, String permission, int slot) {
        Players plyReg = OpenDataBase(p);
        List<String> permissions = YH.getPerks(plyReg.getRanks().getVocation(), plyReg.getRanks().getVocationLvl());
        if (!permissions.contains(permission)) {
            p.sendMessage(ChatColor.RED + "You can't choose that perk.");
            return;
        }
        plugin.Permissions.addUserPermission(p.getWorld().getName(), p.getName(), permission);
        plugin.Permissions.saveAll();
        plugin.Permissions.reload();
        if (slot == 1) {
            plyReg.getRanks().setPerk1(permission);
        }
        if (slot == 2) {
            plyReg.getRanks().setPerk2(permission);
        }
        if (slot == 3) {
            plyReg.getRanks().setPerk3(permission);
        }
        plugin.getDatabase().save(plyReg);
    }

    public void RemovePermission(Player p, String permission) {
        plugin.Permissions.removeUserPermission(p.getWorld().getName(), p.getName(), permission);
        plugin.Permissions.saveAll();
        plugin.Permissions.reload();
    }
}
